{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# SINOGRAM MASHING\n",
    "IT IS DESIRABLE TO REDUCE THE DIMENSIONALITY OF THE EMISSION DATA IN ORDER TO REDUCE THE COMPUTATIONAL COMPLEXITY OF THE RECONSTRUCTION. THIS REQUIRES SOME KIND OF APPROXIMATION. IN THE COMMON APPROACH, THIS IS DONE USING THE <font color=\"#D25C5C\">SINOGRAM MASHING</font> ALGORITHM. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img src=\"./images/michelogram.png\" width=\"700\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## IF POSSIBLE, OCCIPUT AVOIDS SINOGRAM MASHING\n",
    "INSTEAD IT USES A DEDICATED RAY-TRACER"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "<img src=\"./images/ray_tracer_1.png\" width=\"1200\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "THIS APPROACH IS MORE FLEXIBLE AND ENABLES THE USE OF A HIGHLY PARALLEL, RACE-FREE, GPU ACCELERATED RAY-TRACER <br><br>\n",
    "<img src=\"./images/ray_tracer_2.png\" width=\"400\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "IN ORDER TO USE THE SENSITIVITY FILE STORED IN THE SCANNER, HOWEVER, WE HAVE TO TREAT THE EMISSION IN THE SAME WAY AS THE MANUFACTURER. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## SINOGRAM MASHING WITH ARBITRARY SPAN\n",
    "THE NEW VERSION OF OCCIPUT OPTIONALLY EMPLOYS SINOGRAM MASHING"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The library None cannot be FOUND, please make sure that the path to the NiftyRec libraries has been exported. \n",
      "1) Before launching Python, type the following in the terminal (the same terminal): \n",
      "export DYLD_LIBRARY_PATH='path to the niftyrec libraries'\n",
      "Please install NiftyPy\n",
      "The library None cannot be FOUND, please make sure that the path to the NiftyRec libraries has been exported. \n",
      "1) Before launching Python, type the following in the terminal (the same terminal): \n",
      "export DYLD_LIBRARY_PATH='path to the niftyrec libraries'\n",
      "NiftyPy could not be loaded: it will not be possible to reconstruct the PET data. \n"
     ]
    },
    {
     "ename": "LibraryNotFound",
     "evalue": "Library cannot be found: NiftyRec (_et_array_interface) ",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mLibraryNotFound\u001b[0m                           Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-1-a7bb11f49c02>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32mimport\u001b[0m \u001b[0mocciput\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;32m/Users/spedemon/anaconda/lib/python2.7/site-packages/occiput/__init__.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      6\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      7\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 8\u001b[0;31m \u001b[0;32mimport\u001b[0m \u001b[0mCore\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m      9\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mReconstruction\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     10\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mTransformation\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m/Users/spedemon/anaconda/lib/python2.7/site-packages/occiput/Core/__init__.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      8\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      9\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 10\u001b[0;31m \u001b[0;32mfrom\u001b[0m \u001b[0;34m.\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mCore\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     11\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0;34m.\u001b[0m\u001b[0mCore\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mTransform_Affine\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mTransform_Identity\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mTransform_6DOF\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mTransform_Scale\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mTransform_Translation\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mTransform_Rotation\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mGrid3D\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mImage3D\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mGridND\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mImageND\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     12\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0;34m.\u001b[0m\u001b[0mCore\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mgrid_from_box_and_affine\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m/Users/spedemon/anaconda/lib/python2.7/site-packages/occiput/Core/Core.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m     19\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0mocciput\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mglobal_settings\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     20\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0mocciput\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mglobal_settings\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mprintoptions\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 21\u001b[0;31m \u001b[0;32mfrom\u001b[0m \u001b[0mocciput\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mCore\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mNiftyPy_wrap\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mtransform_grid\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mgrid_from_box_and_affine\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mresample_image_on_grid\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mINTERPOLATION_LINEAR\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mINTERPOLATION_POINT\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     22\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0mocciput\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mCore\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mConversion\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mnipy_to_occiput\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mnifti_to_occiput\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mocciput_to_nifti\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mocciput_from_array\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     23\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m/Users/spedemon/anaconda/lib/python2.7/site-packages/occiput/Core/NiftyPy_wrap.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m     14\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mnumpy\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0m__np\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     15\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 16\u001b[0;31m     \u001b[0;32mfrom\u001b[0m \u001b[0mNiftyPy\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mNiftyRec\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mINTERPOLATION_LINEAR\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mINTERPOLATION_POINT\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     17\u001b[0m     \u001b[0;32mfrom\u001b[0m \u001b[0mNiftyPy\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mNiftyRec\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mTR_resample_grid\u001b[0m \u001b[0;32mas\u001b[0m             \u001b[0m__TR_resample_grid\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     18\u001b[0m     \u001b[0;32mfrom\u001b[0m \u001b[0mNiftyPy\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mNiftyRec\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mTR_grid_from_box_and_affine\u001b[0m \u001b[0;32mas\u001b[0m  \u001b[0m__TR_grid_from_box_and_affine\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m/Users/spedemon/anaconda/lib/python2.7/site-packages/NiftyPy/__init__.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m     12\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     13\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0mCommon\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0;34m*\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 14\u001b[0;31m \u001b[0;32mimport\u001b[0m \u001b[0mNiftyRec\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     15\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mNiftyReg\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     16\u001b[0m \u001b[0;31m#import NiftySeg\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m/Users/spedemon/anaconda/lib/python2.7/site-packages/NiftyPy/NiftyRec/__init__.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      9\u001b[0m \u001b[0;31m# Jan. 2014, Boston\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     10\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 11\u001b[0;31m \u001b[0;32mfrom\u001b[0m \u001b[0mNiftyRec\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0;34m*\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;32m/Users/spedemon/anaconda/lib/python2.7/site-packages/NiftyPy/NiftyRec/NiftyRec.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m    126\u001b[0m     \u001b[0;32melif\u001b[0m \u001b[0mplatform\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msystem\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m==\u001b[0m\u001b[0;34m'Windows'\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    127\u001b[0m         \u001b[0;32mprint\u001b[0m \u001b[0;34m\"Add %s to the system PATH using Control Panel -> Advanced Settings -> System -> ..\"\u001b[0m\u001b[0;34m%\u001b[0m\u001b[0mpath\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 128\u001b[0;31m     \u001b[0;32mraise\u001b[0m \u001b[0mLibraryNotFound\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"NiftyRec\"\u001b[0m\u001b[0;34m+\u001b[0m\u001b[0;34m\" (\"\u001b[0m\u001b[0;34m+\u001b[0m\u001b[0mstr\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlibrary_name\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m+\u001b[0m\u001b[0;34m\") \"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    129\u001b[0m \u001b[0;32melif\u001b[0m \u001b[0mfound\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0mFOUND_NOT_LOADABLE\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    130\u001b[0m     \u001b[0;32mprint\u001b[0m \u001b[0;34m\"The library %s cannot be LOADED, please make sure that the path to the NiftyRec libraries has been exported. \"\u001b[0m\u001b[0;34m%\u001b[0m\u001b[0mfullpath\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mLibraryNotFound\u001b[0m: Library cannot be found: NiftyRec (_et_array_interface) "
     ]
    }
   ],
   "source": [
    "import occiput"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "michelogram = occiput.Reconstruction.PET.PET.Michelogram(n_rings=64, span=11, max_ring_difference=60)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "figure(figsize=[18,9])\n",
    "subplot(121); imshow(michelogram.michelogram_sinogram,cmap='coolwarm',interpolation='nearest')\n",
    "subplot(122); imshow(michelogram.michelogram_plane,cmap='Paired',interpolation='nearest')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## HOW TO USE IT?\n",
    "The objects PET_Static_SCAN and PET_Dynamic_SCAN internally have a MICHELOGRAM. Just set the SPAN: "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "pet = occiput.Reconstruction.PET.PET_Static_Scan()\n",
    "pet.set_span(span=11, n_rings=64, max_ring_difference=60)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
